package Tread;

public class Example5_boolean_flag {
    private static volatile boolean flag = false;

    public static void main(String[] args) {
        Thread product = new Thread(() -> {
            try {
                System.out.println("正在生产");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            flag = true;
            System.out.println("product is over!");
        });

        Thread consumer = new Thread(() -> {
            while (!flag) {
                System.out.println("正在等待product...中");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("consumer is over");
        });

        product.start();
        consumer.start();
    }
}
